package org 
{   
	import flash.display.*;
	import flash.events.*;
	import flash.net.*;
	import flash.text.TextField;
	import flash.text.TextFormat;
	
	import mx.events.FlexEvent;
	import mx.preloaders.DownloadProgressBar;
	
	import org.bytearray.gif.player.GIFPlayer; 
	
	
	public class NewPreLoader extends DownloadProgressBar   
	{   
		private var logo:Loader;   
		private var txt:TextField;   
		private var _preloader:Sprite;
		private 	var player:GIFPlayer;
		
		public function NewPreLoader()
		{
			//此方法只能实现显示jpg,png等静态图
			//            logo = new Loader();   
			//            logo.load(new URLRequest("l4cd.png"));   
			//            addChild(logo);   
			
			var request:URLRequest=new URLRequest("assets/images/loader.gif");
			player=new GIFPlayer();
			player.load(request);
			addChild(player);
			
//			var request:URLRequest=new URLRequest("assets/images/newPreLoader.swf");
//			player=new Loader();
//			player.load(request);
			
			addChild(player);
			
			var style:TextFormat=new TextFormat(null, null, 0xFFFFFF, null, null, null, null, null, "center");
			txt=new TextField();
			txt.defaultTextFormat=style;
			txt.width=200;
			txt.selectable=false;
			txt.height=20;
			addChild(txt);
			
			super();
		}   
		//最重要的代码就在这里..重写preloader,让swf执行加载的时候~进行你希望的操作~   
		override public function set preloader(value:Sprite):void{   
			_preloader = value   
			//四个侦听~分别是 加载进度 / 加载完毕 / 初始化进度 / 初始化完毕   
			_preloader.addEventListener(ProgressEvent.PROGRESS,load_progress);   
			_preloader.addEventListener(Event.COMPLETE,load_complete);   
			_preloader.addEventListener(FlexEvent.INIT_PROGRESS,init_progress);   
			_preloader.addEventListener(FlexEvent.INIT_COMPLETE,init_complete);   
			
			stage.addEventListener(Event.RESIZE,resize)   
			resize(null);   
		}   
		private function remove():void{   
			_preloader.removeEventListener(ProgressEvent.PROGRESS,load_progress);   
			_preloader.removeEventListener(Event.COMPLETE,load_complete);   
			_preloader.removeEventListener(FlexEvent.INIT_PROGRESS,init_progress);   
			_preloader.removeEventListener(FlexEvent.INIT_COMPLETE,init_complete);   
			stage.removeEventListener(Event.RESIZE,resize)   
		}   
		private function resize(e:Event):void{   
			player.x = (stage.stageWidth - 40)/2;   
			player.y = (stage.stageHeight-150 )/2;   
			txt.x = (stage.stageWidth - 160)/2;   
			txt.y = player.y + 80+5;   
			
			graphics.clear();   
			graphics.beginFill(0x333333);   
			graphics.drawRect(0,0,stage.stageWidth,stage.stageHeight);   
			graphics.endFill();   
		}   
		private function load_progress(e:ProgressEvent):void{   
			txt.text = "系统已加载"+int(e.bytesLoaded/e.bytesTotal*100)+"%";   
		}   
		private function load_complete(e:Event):void{   
			txt.text = "系统加载完毕!" 
		}   
		private function init_progress(e:FlexEvent):void{   
			txt.text = "系统正在初始化" 
		}   
		private function init_complete(e:FlexEvent):void{   
			txt.text = "系统初始化完毕!" 
			remove()   
			//最后这个地方需要dpe一个Event.COMPLETE事件..表示加载完毕让swf继续操作~   
			dispatchEvent(new Event(Event.COMPLETE))   
		}   
	}   
} 